import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import '../routes.dart';
import '../utils/funtions.dart';
import '../utils/common_toast.dart';
// import '../utils/dio_http.dart';
import '../utils/store.dart';
import '../scoped_model/auth.dart';
import '../utils/scoped_model_helper.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool showPassword = false;
  var usernameController = new TextEditingController();
  var passwordController = new TextEditingController();

  _loginHandle() async {
    var username = usernameController.text;
    var password = passwordController.text;

    if (stringIsNullOrEmpty(username) || stringIsNullOrEmpty(password)) {
      CommonToast.showToast('用户名和密码不能为空！');
      return;
    }

    const url = '/user/login';
    var params = {"username": username, "password": password};
    /* var res = await DioHttp.of(context).post(url, params);
    var resString = json.decode(res.toString());

    int status = resString['status'];
    String msg = resString['description'] ?? "内容为空";
    CommonToast.showToast(msg); */
    // if (status.toString().startsWith('2')) {
    // String token = resString['body']['token'];
    String token = 'skjdchdbafkodfkejdjebhdbchbdchdh';
    Store store = await Store.getInstance();
    await store.setString(StoreKeys.token, token);
    ScopedModelHelper.getModel<AuthModel>(context).login(token, context);
    Timer(Duration(seconds: 1), () {
      Navigator.of(context).pop();
    });
    // }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('登录'),
      ),
      body: SafeArea(
        minimum: EdgeInsets.all(30),
        child: ListView(
          children: [
            TextField(
              controller: usernameController,
              decoration: InputDecoration(labelText: '用户名', hintText: '请输入用户名'),
            ),
            Padding(padding: EdgeInsets.all(10)),
            TextField(
              controller: passwordController,
              obscureText: !showPassword,
              decoration: InputDecoration(
                  labelText: '密码',
                  hintText: '请输入密码',
                  suffixIcon: IconButton(
                    onPressed: () {
                      setState(() {
                        showPassword = !showPassword;
                      });
                    },
                    icon: Icon(
                        showPassword ? Icons.visibility_off : Icons.visibility),
                  )),
            ),
            Padding(padding: EdgeInsets.all(10)),
            // ignore: deprecated_member_use
            RaisedButton(
              color: Colors.green,
              onPressed: () {
                _loginHandle();
              },
              child: Text(
                '登录',
                style: TextStyle(color: Colors.white),
              ),
            ),
            Padding(padding: EdgeInsets.all(10)),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('还没有账号，'),
                // ignore: deprecated_member_use
                FlatButton(
                    onPressed: () {
                      Navigator.pushReplacementNamed(context, Routes.register);
                    },
                    child: Text(
                      '去注册',
                      style: TextStyle(color: Colors.green),
                    ))
              ],
            )
          ],
        ),
      ),
    );
  }
}
